package com.dc.dcit.service;

import com.dc.dcit.common.vo.SysRole;
import com.dc.dcit.common.vo.UserVO;
import com.dc.dcit.jxlunit.SpreadsheetData;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.util.*;

public  class LoadTestDataForUT<T> {
    private static String defaultFileName="C:\\svndata\\ut\\testDataForUT.xls";
//    private static String defaultFileName="src/test/resources/testDataForUT.xls";

    public static <T> List<T> getData(Class<T> clazz,String sheetName,String dataFile)  {
        List<T> list= new ArrayList<>();
        Iterator<Object[]> iterator =null;
        String fileName=dataFile;
        if (null==fileName){
            fileName=defaultFileName;
        }
        try {
            System.out.println("begin to retrieve test data from file:"+ fileName);
            InputStream excelFile = new FileInputStream(new File(fileName));
            Collection<Object[]> spreadsheetData = new SpreadsheetData(excelFile,sheetName).getData();
            iterator = spreadsheetData.iterator();
        }catch (IOException e) {
            e.printStackTrace();
        }
        while(iterator.hasNext()) {
            Object[] row = (Object[]) iterator.next();
            Object cell = row[0];
            JSONObject object =JSONObject.fromObject(cell);
            Map<String,Class<?>> classMap = new HashMap<String,Class<?>>();
            classMap.put("roleList", SysRole.class);
            T value=(T) JSONObject.toBean(object, clazz,classMap);
            list.add(value);
        }
        System.out.println("Interface retrieved for class:"+clazz.getName());
        return list;
    }
}